<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:ddl="livcos.org/ns/db/ddl"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="c doc ddl pm f v">
  
  <xsl:import href="../../../../../base.xsl" />
  
  <xsl:template match="/ddl:table">
  	<html id="{$callerObjId}">
  		<head>
  			<title>Table <xsl:value-of select="@name" /></title>
  		</head>
  		<body>
  			<xsl:apply-templates select="." mode="content"/>
  		</body>
  	</html>
  </xsl:template>
	
	<xsl:template match="ddl:table">
		<xsl:apply-templates select="." mode="content"/>
	</xsl:template>
	
	<xsl:template match="ddl:table" mode="content">
		<v:style ref="{resolve-uri('styles.css')}"/>
		<a name="{@id}" />
    <h1>Tabelle <xsl:value-of select="@name" /></h1>
    <table class="table_overview">
      <thead>
        <tr>
          <th>Feld</th>
          <th>Type</th>
          <th>Dimension</th>
          <th>Default</th>
          <th>Shortcut</th>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="ddl:field" mode="overview">
          <xsl:sort select="@name = ../ddl:key/ddl:field/@ref-name" order="descending"/>
          <xsl:sort select="@name"/>
        </xsl:apply-templates>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="5" class="table_legend">
            <span class="primaryKey">&#160;&#160;</span> Primary Key&#160;&#160; <span class="mandatory"
              >&#160;&#160;</span> Mandatory&#160;&#160; <span class="nullable">&#160;&#160;</span> Optional (allow NULL) </td>
        </tr>
      </tfoot>
    </table>
  	<xsl:if test="ddl:key/ddl:referer">
  		<h2>Referenzierende Tabellen</h2>
  		<p class="block">
  			<xsl:for-each-group select="ddl:key/ddl:referer" group-by="@ref-table">
  				<xsl:if test="position() > 1">, </xsl:if>
  				<a href="{f:cosmos-uri(@ref-table, .)/obj/@name}.html">
  					<xsl:value-of select="@ref-table-name"/>
  				</a>
  			</xsl:for-each-group>
  		</p>
  	</xsl:if>
		<div id="{f:object-id(.)}">
			<h2>Beschreibung</h2>
			<xsl:apply-templates select="doc:* except (doc:link | doc:concept-link | doc:report-link | doc:validation-link)"/>
			<xsl:apply-templates select="pm:todo"/>
			<xsl:variable name="links" select="doc:link | doc:concept-link | doc:report-link | doc:validation-link"/>
			<xsl:if test="$links">
				<h3>Siehe auch...</h3>
				<ul>
					<xsl:apply-templates select="$links"/>
				</ul>
			</xsl:if>
		</div>
		<h2>Felder</h2>
    <xsl:apply-templates select="ddl:field">
      <xsl:sort select="@name = ../ddl:key/ddl:field/@ref-name" order="descending" />
      <xsl:sort select="@name" />
    </xsl:apply-templates>
    <xsl:if test="ddl:unique | ddl:check">
      <h2>Constraints</h2>
      <table cellspacing="0" cellpadding="4" border="1">
        <thead>
          <tr>
            <th>Constraint</th>
            <th>Type</th>
            <th>Felder</th>
            <th>Beschreibung</th>
          </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="ddl:unique | ddl:check">
            <xsl:sort select="@name"/>
          </xsl:apply-templates>
        </tbody>
      </table>
    </xsl:if>
    <xsl:if test="ddl:trigger">
      <h2>Trigger</h2>
      <xsl:apply-templates select="ddl:trigger"/>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="ddl:field" mode="overview">
    <tr>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="../ddl:key/ddl:field[@ref-name = current()/@name]">primaryKey</xsl:when>
          <xsl:when test="@nullable">nullable</xsl:when>
          <xsl:otherwise>mandatory</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <td>
        <a href="#{@id}"><xsl:value-of select="@name"/></a>
      </td>
      <td align="center">
        <xsl:value-of select="@dbtype" />
      </td>
      <td align="center">
        <xsl:choose>
          <xsl:when test="@size and @scale"><xsl:value-of select="@size - @scale"/>,<xsl:value-of select="@scale"/></xsl:when>
          <xsl:when test="@size"><xsl:value-of select="@size"/></xsl:when>
          <xsl:otherwise>&#160;</xsl:otherwise>
        </xsl:choose>
      </td>
      <td>
        <xsl:if test="@type != 'varchar'">
          <xsl:attribute name="align">right</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="@default"/><xsl:if test="not(@default)">&#160;</xsl:if>
      </td>
      <td>
        <xsl:variable name="refs" select="../ddl:ref[ddl:field/@ref-name = current()/@name]"/>
        <xsl:apply-templates select="$refs" mode="shortcut"/>
        <xsl:if test="not($refs)">&#160;</xsl:if>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="ddl:field">
    <div id="{f:object-id(.)}#{@id}">
      <h3>
        <a name="{@id}"/>
        <xsl:value-of select="@name"/>
      </h3>
    	<xsl:apply-templates select="doc:*"/>
    	<p class="block">
    		<xsl:text>Einträge für das </xsl:text>
    		<xsl:if test="@nullable">optionale</xsl:if> Feld '<xsl:value-of select="@name"/>
    		<xsl:text>' vom Typ </xsl:text>
    		<span class="text_type">
          <xsl:value-of select="@dbtype"/>
          <xsl:if test="not(@dbtype)">
            <xsl:value-of select="@type"/>
          </xsl:if>
          <xsl:choose>
            <xsl:when test="@size and @scale">(<xsl:value-of select="@size - @scale"/>,<xsl:value-of select="@scale"/>)</xsl:when>
            <xsl:when test="@size">(<xsl:value-of select="@size"/>)</xsl:when>
          </xsl:choose>
        </span>
        <xsl:choose>
          <xsl:when test="../ddl:key/ddl:field[@ref-name = current()/@name]"> bilden den Primary-Key der Tabelle.</xsl:when>
          <xsl:when test="@default"> werden mit <span class="text_value">
              <xsl:value-of select="@default"/>
            </span> als Defaultwert erstellt.</xsl:when>
          <xsl:otherwise> werden ohne Defaultwert erstellt.</xsl:otherwise>
        </xsl:choose>
      </p>
      <xsl:apply-templates select="../ddl:ref[ddl:field/@ref-name = current()/@name]"/>
    	<xsl:apply-templates select="pm:todo"/>
    	<xsl:variable name="links" select="doc:link | doc:concept-link | doc:report-link | doc:validation-link"/>
    	<xsl:if test="$links">
    		<h3>Siehe auch...</h3>
    		<ul>
    			<xsl:apply-templates select="$links"/>
    		</ul>
    	</xsl:if>
    </div>
  </xsl:template>
  
  <xsl:template match="ddl:ref" mode="shortcut">
  	<a href="{f:cosmos-uri(@key-table, .)/obj/@name}.html"><xsl:value-of select="@key-table-name"/></a>
  </xsl:template>
  
  <xsl:template match="ddl:ref">
    <p class="block">
      Das Feld referenziert einen Record aus der 
			<a href="{f:cosmos-uri(@key-table, .)/obj/@name}.html">
				<xsl:value-of select="@key-table-name"/>
			</a> Tabelle.
      <xsl:if test="@update | @delete">
        Die referenzielle Integrität wird für
        <xsl:choose>
        	<xsl:when test="@update and @delete">Updates und Deletes</xsl:when>
        	<xsl:when test="@update">Updates</xsl:when>
        	<xsl:when test="@delete">Deletes</xsl:when>
        </xsl:choose>
        <xsl:text>automatisch geprüft</xsl:text>
        <xsl:variable name="cascade-action">
        	<xsl:choose>
        		<xsl:when test="(@update = 'cascade') and (@delete = 'cascade')">Updates und Deletes</xsl:when>
        		<xsl:when test="@update = 'cascade'">Updates</xsl:when>
        		<xsl:when test="@delete = 'cascade'">Deletes</xsl:when>
        	</xsl:choose>
        </xsl:variable>
        <xsl:if test="$cascade-action">
        	<xsl:text> (</xsl:text>
        	<xsl:value-of select="$cascade-action"/>
        	<xsl:text> werden propagiert)</xsl:text>
        </xsl:if>
        <xsl:text>.</xsl:text>
      </xsl:if>
    </p>
  </xsl:template>
  
  <xsl:template match="ddl:unique | ddl:check">
    <tr id="{f:object-id(.)}#{@id}">
      <td>
        <a name="{@id}" />
        <xsl:value-of select="@name" />
      </td>
      <td>
        <xsl:value-of select="local-name(.)" />
      </td>
      <td>
        <xsl:apply-templates select="ddl:field" mode="list">
          <xsl:sort select="@ref-name" />
        </xsl:apply-templates>
      </td>
      <td>
        <xsl:apply-templates select="doc:*"/>
        <xsl:if test="not(doc:*)">&#160;</xsl:if>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="ddl:field" mode="list">
    <xsl:if test="position() > 1"><br/></xsl:if>
    <xsl:variable name="fld" select="../../ddl:field[@name = current()/@ref-name]"/>
    <a href="#{$fld/@id}"><xsl:value-of select="$fld/@name"/></a>
  </xsl:template>
  
  <xsl:template match="ddl:trigger">
    <div id="{f:object-id(.)}#{@id}">
      <h3>
        <xsl:value-of select="@name"/>
      </h3>
      <xsl:apply-templates select="doc:*"/>
      <pre class="code block"><xsl:value-of select="ddl:def"/></pre>
    </div>
  </xsl:template>
  
</xsl:stylesheet>
